<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== essentialmind ==--
    *
    * @version n/a
    * @since 09.05.2011
    * @copyright essentialmind 2011
    *
    *********************************************************/

    /***
    * Class installNodeEstate
    ****
    * This is the module installer/deinstaller, shall
    * automate integration of new modules into client
    * solutions
    ****
    * @package installNodes
    * @author Thomas Brandstätter <tb@essentialmind.com>
    * @version n/a
    * @since 09.05.2011
    * @copyright essentialmind 2011
    ***/
    class installNodeEstate extends installNode{
        
        /**
         * This is the node install script
         *
         */
        protected function create(){
            //create database table  
                    
            $this->db->Execute("CREATE TABLE estate_federals(
                                    id              INTEGER UNIQUE,
                                    name            VARCHAR(50),
                                    PRIMARY KEY(id)
                                )");
                                
            $this->db->Execute("INSERT INTO estate_federals VALUES
                                    (1, 'Wien'),
                                    (2, 'Niederöstereich'),
                                    (3, 'Burgenland'),
                                    (4, 'Steiermark'),
                                    (5, 'Kärnten'),
                                    (6, 'Oberösterreich'),
                                    (7, 'Salzburg'),
                                    (8, 'Tirol'),
                                    (9, 'Vorarlberg')");   
                                    
                                    
            $this->db->Execute("CREATE TABLE estate_types(
                                    id          SERIAL,
                                    name        VARCHAR(50),
                                    PRIMARY KEY(id)
                                )");
                                
            $this->db->Execute("INSERT INTO estate_types(name) VALUES
                                    ('Haus'),
                                    ('Grundst&uuml;ck'),
                                    ('Wohnung'),
                                    ('B&uuml;ro'),
                                    ('Gastronomie')");

                                
                                    
            $this->db->Execute("CREATE TABLE estate_kinds(
                                    id          SERIAL,
                                    name        VARCHAR(50),
                                    PRIMARY KEY(id)
                                )");

            $this->db->Execute("INSERT INTO estate_kinds(name) VALUES
                                    ('Eigentum'),
                                    ('Hauptmiete'),
                                    ('Untermiete'),
                                    ('befristete Miete')");


                                             
            $this->db->Execute("CREATE TABLE estate_accountrements(
                                    id          SERIAL,
                                    name        VARCHAR(50),
                                    PRIMARY KEY(id))");

            $this->db->Execute("INSERT INTO estate_accountrements(name) VALUES
                                    ('M&ouml;bliert'),
                                    ('Teilm&ouml;bliert'),
                                    ('K&uuml;che')");

      
            
            $this->db->Execute("CREATE TABLE estate_floors(
                                    id          SERIAL,
                                    name        VARCHAR(50),
                                    PRIMARY KEY(id))");
                                    
            $this->db->Execute("INSERT INTO estate_floors(name) VALUES
                                    ('Parkett'),
                                    ('Laminat'),
                                    ('PVC'),
                                    ('Teppich')");


                                    
            $this->db->Execute("CREATE TABLE estate_windows(
                                    id          SERIAL,
                                    name        VARCHAR(50),
                                    PRIMARY KEY(id))");

            $this->db->Execute("INSERT INTO estate_windows(name) VALUES
                                    ('Kunststoff'),
                                    ('Holz'),
                                    ('Aluminium')");



            $this->db->Execute("CREATE TABLE estate_heatings(
                                    id          SERIAL,
                                    name        VARCHAR(50),
                                    PRIMARY KEY(id))");                      
                                    
            $this->db->Execute("INSERT INTO estate_heatings(name) VALUES
                                    ('&Ouml;l'),
                                    ('Gas'),
                                    ('Strom'),
                                    ('Fernw&auml;rme'),
                                    ('Feste Brennstoffe')");     
                                    
            $this->db->Execute("CREATE TABLE estate_states(
                                    id          SERIAL,
                                    name        VARCHAR(50),
                                    PRIMARY KEY(id))");
                                    
            $this->db->Execute("INSERT INTO estate_states(name) VALUES
                                    ('Sehr gut'),
                                    ('Renovierungsbed&uuml;rfdig'),
                                    ('Neu saniert'),
                                    ('Erstbezug')");     
                                                                                                       

            $this->db->Execute("CREATE TABLE node_estate(
                                    id              SERIAL,
                                    type_id         INTEGER REFERENCES estate_types(id),
                                    kind_id         INTEGER REFERENCES estate_kinds(id),         
                                    federal_id      INTEGER REFERENCES estate_federals(id),
                                    groundplan_id   INTEGER,
                                    heating_id      INTEGER REFERENCES estate_heatings(id),
                                    window_id       INTEGER REFERENCES estate_windows(id),
                                    floor_id        INTEGER REFERENCES estate_floors(id),
                                    accountrements_id INTEGER REFERENCES estate_accountrements(id),
                                    state_id        INTEGER REFERENCES estate_states(id),
                                    city            VARCHAR(100),
                                    zip             VARCHAR(12),
                                    address         VARCHAR(255),
                                    price           INTEGER,
                                    space           INTEGER,
                                    rooms           INTEGER,
                                    storey          INTEGER,
                                    category        VARCHAR(1),
                                    obtainable_instantly BOOLEAN DEFAULT '1' NOT NULL,
                                    obtainable_at   DATE,
                                    bath_rooms      INTEGER,
                                    toilets         INTEGER,
                                    terraces        INTEGER,
                                    balconies       INTEGER,
                                    loggia          INTEGER,
                                    terraces_sm     INTEGER,
                                    balconies_sm    INTEGER,
                                    loggia_sm       INTEGER,
                                    content         TEXT,
                                    PRIMARY KEY(id)
                                )");
                                
            $this->db->Execute("CREATE TABLE estate_pictures(
                                    id              SERIAL,
                                    width           INTEGER,                                                    
                                    height          INTEGER,
                                    filesize        INTEGER,
                                    estate_id       INTEGER REFERENCES node_estate(id),
                                    order_pos       INTEGER DEFAULT 0,
                                    PRIMARY KEY(id)
                                )");  
                                
            $this->db->Execute("CREATE INDEX estate_pictures_order_idx ON estate_pictures(id, order_pos)");
            $this->db->Execute("CREATE INDEX estate_ ON estate_pictures(id, order_pos)");
            
            $this->db->Execute("ALTER TABLE node_estate
                                    ADD CONSTRAINT estate_node_ground_plan_id_fkey 
                                    FOREIGN KEY (groundplan_id) REFERENCES estate_pictures(id)");
        }
        
        /**
         * This is the node uninstall script
         *
         */
        protected function remove(){
            //drop database table   
            
            $this->db->Execute("ALTER TABLE node_estate DROP CONSTRAINT estate_node_ground_plan_id_fkey");
            $this->db->Execute("DROP TABLE estate_pictures");
            $this->db->Execute("DROP TABLE node_estate");
            $this->db->Execute("DROP TABLE estate_states");
            $this->db->Execute("DROP TABLE estate_heatings");
            $this->db->Execute("DROP TABLE estate_windows");
            $this->db->Execute("DROP TABLE estate_floors");
            $this->db->Execute("DROP TABLE estate_accountrements");
            $this->db->Execute("DROP TABLE estate_kinds");
            $this->db->Execute("DROP TABLE estate_types");
            $this->db->Execute("DROP TABLE estate_federals");
        }
        
        
        /**
         * Sets the properties of this node
         *
         */
        protected function setProps(){
            $this->setDescription('Estate');   
            $this->setAdminAdd(true);
            $this->setIsFolder(false);
        }
    }
?>